﻿namespace DeltaGroup.WheelOfJeopardy.ChallengeManager
{
    using System.Collections.Generic;
    using System.Linq;

    using DeltaGroup.WheelOfJeopardy.Model;

    public class ModerateStrategy : InterpreterStrategy
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ModerateStrategy"/> class.
        /// </summary>
        internal ModerateStrategy() {
            this.StopWordList = StopWords.ModerateWordList;
        }

        #region Methods

        /// <summary>
        /// Evaluates the response using moderate rules. Checks for Synonym matches.
        /// </summary>
        /// <param name="challenge">The challenge.</param>
        /// <param name="responseString">The response string.</param>
        /// <returns></returns>
        public override bool EvaluateResponse(Challenge challenge, string responseString)
        {
            bool acceptableResponse;

            acceptableResponse = ExactCheck(challenge.ExpectedResponse.ToLower(), responseString.ToLower());
            if (!acceptableResponse)
            {
                Phrase expectedResponse = new Phrase(challenge.ExpectedResponse.ToLower(), StopWordList);
                Phrase actualResponse = new Phrase(responseString.ToLower(), StopWordList);

                acceptableResponse = OrderedTokenCheck(expectedResponse, actualResponse);

                if (!acceptableResponse)
                {
                    //Breaks the Synonym serialization if constructor added.
                    List<Synonym> synonymList = challenge.SynonymList.Select(
                                        s => new ConstructableSynonym(s.Token.ToLower(), s.Alternate.ToLower()) as Synonym).ToList();

                    PhraseMatchResult matchResult = SynonymMatchCheck(expectedResponse, actualResponse, synonymList);
                    acceptableResponse = (matchResult.MatchedTokens == expectedResponse.TokenCount);
                }
            }

            return acceptableResponse;
        }

        #endregion Methods
    }
}